using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Services
{
    /// <summary>
    /// 安全事件服务实现
    /// 提供安全事件管理的业务逻辑实现，包括安全事件的查询、添加、更新和删除操作
    /// </summary>
    public class SecurityEventService : ISecurityEventAppService
    {
        /// <summary>
        /// 安全事件仓储接口
        /// </summary>
        private readonly Domain.Interfaces.ISecurityEventRepository _repo;
        
        /// <summary>
        /// 初始化安全事件服务实例
        /// </summary>
        /// <param name="repo">安全事件仓储接口实例</param>
        public SecurityEventService(Domain.Interfaces.ISecurityEventRepository repo)
        {
            _repo = repo;
        }

        /// <summary>
        /// 获取所有安全事件记录
        /// 检索系统中的所有安全事件信息并转换为DTO格式
        /// </summary>
        /// <returns>安全事件DTO列表</returns>
        public async Task<IEnumerable<Application.DTOs.SecurityEventDto>> GetAllAsync()
        {
            var entities = await _repo.GetAllAsync();
            return entities.Select(e => new Application.DTOs.SecurityEventDto
            {
                Id = e.Id,
                Type = e.EventType,
                Description = e.Description,
                EventTime = e.CreatedAt,
                UserId = e.UserId 
            });
        }

        /// <summary>
        /// 根据ID获取安全事件记录
        /// 查找指定标识符的安全事件详细信息
        /// </summary>
        /// <param name="id">安全事件标识符</param>
        /// <returns>安全事件DTO，如果不存在则返回null</returns>
        public async Task<Application.DTOs.SecurityEventDto?> GetByIdAsync(long id)
        {
            var entity = await _repo.GetByIdAsync(id);
            if (entity == null) return null;
            return new Application.DTOs.SecurityEventDto
            {
                Id = entity.Id,
                Type = entity.EventType,
                Description = entity.Description,
                EventTime = entity.CreatedAt,
                UserId = entity.UserId ?? string.Empty
            };
        }

        /// <summary>
        /// 添加新的安全事件记录
        /// 创建新的安全事件信息并保存到数据存储
        /// </summary>
        /// <param name="dto">安全事件DTO对象</param>
        /// <returns>添加操作的异步任务</returns>
        public async Task AddAsync(Application.DTOs.SecurityEventDto dto)
        {
            var entity = new Domain.Entities.SecurityEvent
            {
                Id = dto.Id,
                EventType = dto.Type,
                Description = dto.Description,
                CreatedAt = dto.EventTime,
                UserId = dto.UserId
            };
            await _repo.AddAsync(entity);
        }

        /// <summary>
        /// 更新安全事件记录
        /// 更新指定安全事件的信息
        /// </summary>
        /// <param name="id">安全事件标识符</param>
        /// <param name="dto">更新后的安全事件DTO对象</param>
        /// <returns>更新操作的异步任务</returns>
        public async Task UpdateAsync(long id, Application.DTOs.SecurityEventDto dto)
        {
            var entity = new Domain.Entities.SecurityEvent
            {
                Id = dto.Id,
                EventType = dto.Type,
                Description = dto.Description,
                CreatedAt = dto.EventTime,
                UserId = dto.UserId
            };
            await _repo.UpdateAsync(id, entity);
        }

        /// <summary>
        /// 删除安全事件记录
        /// 从系统中移除指定的安全事件信息
        /// </summary>
        /// <param name="id">安全事件标识符</param>
        /// <returns>删除操作的异步任务</returns>
        public async Task DeleteAsync(long id)
        {
            await _repo.DeleteAsync(id);
        }
    }
}
